{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fed929a4",
   "metadata": {},
   "source": [
    "- 1.记录人：杨茜\n",
    "- 2.记录时间：2021-11-29\n",
    "- 3.记录内容：函数与模块python\n",
    "\n",
    "# 函数的定义\n",
    "\n",
    "- 定义一个函数的一般步骤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e123ffc7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0112358\n"
     ]
    }
   ],
   "source": [
    "a,b = 0,1\n",
    "while a < 10:\n",
    "    print(a, end='')  #01\n",
    "    a,b = b,a+b # a=1 a=1 b=2 b=2 a=2 b=3\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31035de6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):  # write Fibonacci series up to n\n",
    "    \"\"\"\"print a Fibonacci series up to n\"\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b60a2ad3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01123581321345589\n"
     ]
    }
   ],
   "source": [
    "a,b = 0,1\n",
    "while a < 100:\n",
    "    print(a, end='')  #01\n",
    "    a,b = b,a+b # a=1 a=1 b=2 b=2 a=2 b=3\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb09bd15",
   "metadata": {},
   "source": [
    "# ......我想重复使用某一个功能（避免重复造轮子）\n",
    "# ......使用者只需会调用函数即可 function_name()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ca99c9f",
   "metadata": {},
   "source": [
    "> 1.定义函数的一般方法：\n",
    "...\n",
    "### def function_name() : \n",
    "-  1.function_name 遵循变量定义的规则 区别数学函数==> f(x) python的函数可以自定义名称                     \n",
    "-  2.函数的注释可以和使用者交流 ：\"\"\"函数的注释\"\"\"          \n",
    "-  3.函数体要和定义函数保持一定的tab缩进：函数体  \n",
    "...   \n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "25740ff2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):  # write Fibonacci series up to n\n",
    "  #  \"\"\"\"print a Fibonacci series up to n\"\"\"\"\n",
    "    a,b = 0,1\n",
    "    while a < n:\n",
    "        print(a, end='')  #0 1 2 1 2\n",
    "        a,b = b,a+b # a=1 a=1 b=2 b=2 a=2 b=3\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7b16cf0",
   "metadata": {},
   "source": [
    "> # 函数的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d2025f54",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0112358\n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "682531f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0112358132134\n"
     ]
    }
   ],
   "source": [
    "fib(50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1ba27af4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "01123581321345589\n"
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ccfd1d85",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.实现hello world\n",
    "def hello():\n",
    "    # \"\"\"打印 你好 世界\"\"\"\n",
    "    print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e0dc8d7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2c52cc27",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.实现hello username 你好 同学\n",
    "def hello_user():\n",
    "    # \"\"\"打印 你好 用户名\"\"\"\n",
    "    print(\"你好，\" + username.title()+\"同学\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4caf919c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'username' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-25-51d81f67aa4b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhello_user\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-23-8ca1b69da501>\u001b[0m in \u001b[0;36mhello_user\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mhello_user\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;31m# \"\"\"打印 你好 用户名\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"你好，\"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0musername\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtitle\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;34m\"同学\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'username' is not defined"
     ]
    }
   ],
   "source": [
    "hello_user()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d598625",
   "metadata": {},
   "source": [
    "## 3.函数的参数\n",
    "\n",
    "> 1. 形式参数：定义参数时，写在function_name括号里面的参数\n",
    ">> A. 在定义函数时，形式参数不占空间\n",
    "\n",
    "> 2. 实际参数：调用参数时，写function_name在括号里面的参数\n",
    ">> B.在使用函数时，形式参数被分配空间，变为实际参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7b733ee",
   "metadata": {},
   "source": [
    "* 当函数具有多个参数时\n",
    "\n",
    "> 1.位置参数               \n",
    ">> 1.形式参数有先后顺序，函数调用过程中要考虑参数的先后顺序，避免出现顺序错误的笑话                      \n",
    ">> 2.调用函数过程中要考虑形式参数的个数   \n",
    "\n",
    "> 2.关键字参数                            \n",
    ">> 1.调用函数时，通过赋值的方式传递给形参变量数据                  \n",
    ">> 2.关键字参数不考虑先后顺序，因为是指定变量赋值                          \n",
    "\n",
    "> 3.默认参数                         \n",
    ">>1.提前预设一个用户可能使用的数值                              \n",
    ">>2.用户在调用函数时，可以省略不给默认参数赋值                               \n",
    ">>3.用户默认参数赋值，将其替换默认参数的值                      \n",
    "\n",
    "\n",
    "# 在使用 describe_pet() 函数时 需要考虑参数的位置 从而进行参数数据的输入（实际参数的调用）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7f9273a",
   "metadata": {},
   "outputs": [],
   "source": [
    "describe_pet(\"dog\",\"嘟嘟\") # 要根据形式参数的位置先后顺序 先写animal_type. 再写pet_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed0b5a8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "describe_pet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0a5a9e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关键字参数使用\n",
    "\n",
    "describe_pet(pet_name=\"嘟嘟\",animal_type=\"dog\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a87792b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 默认参数\n",
    "def describe_pet(pet_name,animal_type=\"dog\"): # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "930a55a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "describe_pet(\"嘟嘟\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b6539a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "describe_pet(\"嘟嘟\",\"dog\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1d03bad",
   "metadata": {},
   "source": [
    "* 关键字参数能否和位置参数一起使用？\n",
    "* 1.可以，但位置必须位于关键字之前（语法顺序）\n",
    "* 2.映射时不能出现多重值（multiple values）test1() got multiple values for argument 'x'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8cf90d50",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 案例1\n",
    "def test1(x,y,z):\n",
    "    \"\"\"打印x,y,z的值\"\"\"\n",
    "    print(x)\n",
    "    print(y)\n",
    "    print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d49b88c3",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (<ipython-input-33-fd583303ea9b>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-33-fd583303ea9b>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    test1(x=2,3,z=4)\u001b[0m\n\u001b[1;37m              ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "test1(x=2,3,z=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "53c8d12d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "test1(2,z=3,y=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b32cebe1",
   "metadata": {},
   "outputs": [],
   "source": [
    "test1(2,x=3,y=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f6c200c",
   "metadata": {},
   "source": [
    "## 课本练习实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e83fe9ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    vowels = set(\"aeiou\")\n",
    "    word = set(word)\n",
    "    print(vowels & word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ddc07bbc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'e', 'a'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"aSDasfaefgergtrgszdeeet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ba6f628e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'o'}\n"
     ]
    }
   ],
   "source": [
    "search4vowels(\"word\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a246c9d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
